package com.mk.web.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.mk.web.model.People;
import com.mk.web.service.PeopleService;

/**
 * Created by WangChen on 2018/11/28 18:48.
 */
@WebServlet("/people")
public class PeopleServlet extends HttpServlet {

    private PeopleService peopleService;

    @Override
    public void init() throws ServletException {
        WebApplicationContext webApplicationContext =
                WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        peopleService = webApplicationContext.getBean("peopleService", PeopleService.class);

    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name = req.getParameter("name");
        List<People> peopleList = peopleService.findByName(name);
        for (People people : peopleList){
            System.out.println(people);
        }

        req.setAttribute("list", peopleList);
    }
}
